package classes
{
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.containers.Form;
	import mx.containers.FormItem;
	import mx.containers.TitleWindow;
	import mx.controls.ComboBox;
	import mx.controls.TextInput;
	import mx.events.CloseEvent;
	import mx.utils.StringUtil;
	import mx.validators.StringValidator;
	

	public class FormPersonalizado extends TitleWindow
	{
		private var _form:Form;
		private var _canvas:Canvas;
		private var _linha:int;
		private var _coluna:int;
		
		public function FormPersonalizado(width:int, height:int)
		{
			this._form = new Form();
			this._form.width = width - 50;
			this._form.height = height - 50;
			
			_canvas = new Canvas();
			_canvas.width = this._form.width - 40;
			_canvas.height = this._form.height - 40;
			
			this.width = width;
			this.height = height;
			this.showCloseButton = true;
			this.addEventListener(CloseEvent.CLOSE, close);
			
			this._linha = 10;
			this._coluna = 0;
		}
		private function close(evt:CloseEvent):void
		{
			dispatchEvent(new Event("fecha_janela", true));
		}
		
		private function mudarColuna(tamanho:int):void
		{
			if (tamanho + _linha > this._canvas.width - 30)
			{
				this._linha = 10;
				this._coluna += 50;
			}
		}
		private function mudarLinha(tamanho:int):void
		{
			_linha += tamanho + 100;
		}
		
		public function addInput(descricao:String, id:String, tamanho:int, maxChars:int, required:Boolean, fontColor:String="#000000"):void
		{
			var input:TextInput = new TextInput();
			input.id = id;
			input.maxChars = maxChars;
			input.width = tamanho;
			input.setStyle("color", fontColor);
			input.stylesInitialized();
			
			mudarColuna(tamanho);
			
			if (descricao.length > 0)
			{
				var formItem:FormItem = new FormItem();
				formItem.label = descricao;
				formItem.addChild(input);
				formItem.required = required;
				formItem.x = _linha;
				formItem.y = _coluna;
				this._canvas.addChild(formItem);
			}
			else
			{
				input.x = _linha;
				input.y = _coluna;
				this._canvas.addChild(input);
			}
			
			mudarLinha(tamanho);
		}
		
		public function addListOption(list:ArrayCollection):void
		{
			for each(var item:Object in list)
			{
				
			}
		}
		
		public function MontarForm():FormPersonalizado
		{
			this._form.addChild(_canvas);
			this.addChild(this._form);
			
			return this;
		}
	}
}